/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.pk;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;
import com.yunji.framework_template.common.util.NumberUtil;

/**
 * ClassName:NationNewsCrawler <br/>
 * Date: 2018年12月18日 下午3:31:24 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class UrdupointNewsCrawler extends NewsCrawler {

    @Resource
    private UrdupointNewsImageHandler urdupointNewsImageHandler;

    @Override
    public boolean isOkUrl(String url) {
        // 通过判断url的path中是否包括年月来判断其是否为新闻详情页
        URI uri;
        try {
            if (url == null || url.trim().length() == 0) {
                return false;
            }
            if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
                return false;
            }
            uri = new URI(url);
            String host = uri.getHost();
            if (host.indexOf("urdupoint.com") < 0) {
                return false;
            }
            String path = uri.getPath();
            String[] pathArr = path.split("/");
            if (pathArr.length >= 4 && url.endsWith(".html") && url.indexOf("/names/") < 0) {
                String last = pathArr[pathArr.length - 1].replaceAll(".html", "");
                if (last.split("-").length >= 2 || NumberUtil.isNumber(last)) {
                    return true;
                }

            }
        } catch (URISyntaxException e) {
        }
        return false;
    }

    @Override
    public ContentImageHandler getContentImageHandler() {
        return urdupointNewsImageHandler;
    }

    @Override
    public List<SourceType> getSourceTypeList() {
        List<SourceType> sourceTypeList = new ArrayList<SourceType>();
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/daily/").newsType(NewsType.EDUCATION).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/pakistan/").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/international/").newsType(NewsType.IMAGE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/poetry/").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/kids/").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/women/").newsType(NewsType.FUN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/cooking/").newsType(NewsType.HOUSE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/showbiz/").newsType(NewsType.ASKANDANSWER).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/horoscope/").newsType(NewsType.HEALTH).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/sports/").newsType(NewsType.MILITARY).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/islam/").newsType(NewsType.CAR).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/health/").newsType(NewsType.TECH).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/books/").newsType(NewsType.FINANCE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/technology/").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/business/").newsType(NewsType.BUSINESS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/mobile/").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/dictionary/").newsType(NewsType.BLOG).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/weird/").newsType(NewsType.MAN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/politics/").newsType(NewsType.WOMAN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/videos/").newsType(NewsType.KIDS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/education/").newsType(NewsType.FOOD).build());

        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/saudi-arabia").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/united-arab-emirates").newsType(NewsType.EDUCATION).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/oman").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/iran").newsType(NewsType.IMAGE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/bahrain").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/iraq").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/qatar").newsType(NewsType.FUN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/kuwait").newsType(NewsType.HOUSE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/egypt").newsType(NewsType.ASKANDANSWER).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/baghdad").newsType(NewsType.HEALTH).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/dubai").newsType(NewsType.MILITARY).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/tehran").newsType(NewsType.CAR).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/riyadh").newsType(NewsType.TECH).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/cairo").newsType(NewsType.FINANCE).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/jeddah").newsType(NewsType.NEWS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/sharjah").newsType(NewsType.BUSINESS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/makkah").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/doha").newsType(NewsType.BLOG).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/kuwait-city").newsType(NewsType.MAN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/dukhan").newsType(NewsType.WOMAN).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/abu-dhabi").newsType(NewsType.KIDS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/medina").newsType(NewsType.FOOD).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/manama").newsType(NewsType.POLITICS).build());
        sourceTypeList.add(SourceType.builder().url("https://www.urdupoint.com/muscat").newsType(NewsType.HOT).build());

        return sourceTypeList;
    }

    @Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.PK.name());
        return countryCodeSet;
    }
}
